package br.com.acme.transaction;

import br.com.acme.user.User;

import static org.mockito.Mockito.*

before "start dependencies", {
	validationService = mock(ValidationService.class)
	paymentRepository = mock(PaymentRepository.class)
	paymentService = new PaymentServiceImpl()

	paymentService.validationService = validationService
	paymentService.paymentRepository = paymentRepository
}

scenario "a successful payment", {

	given "an user, assuming the role of a debtor, wants to transfer money" , {
		debtor = new User("joe@phoenix.com")
		paymentValue = new BigDecimal("90")
	}

	and "the creditor is also a platform user", {
		creditor = new User("jane@phoenix.com")
	}

	and "debtor is eligible to transfer " , {
		when(validationService.conformsTo(debtor)).thenReturn(Boolean.TRUE);	
	}

	and "debtor has balance ", {
		debtorBalance = paymentValue.add(BigDecimal.ONE)
		when(paymentRepository.findBalance(debtor)).thenReturn(debtorBalance);
	}

	when "a payment of $paymentValue is requested from ${debtor.email} to ${creditor.email}", {
		paymentService.transferFrom(debtor, creditor, paymentValue)
	}

	then "${debtor.email}'s balance should have been deducted and ${creditor.email}'s should be credited", {
		verify(paymentRepository).addBalance(debtor  , paymentValue.negate());
		verify(paymentRepository).addBalance(creditor, paymentValue);
	}

}
